package com.crm.consult.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.crm.model.BizConfig;
import com.crm.model.BizProduct;
import com.crm.model.BizProject;
import com.crm.model.BizProjectLevel;
import com.crm.model.ConsultInfoManage;
import com.crm.model.ConsultInfoManagePay;
import com.crm.model.SysUser;
import com.crm.service.BizConfigService;
import com.crm.service.BizProductService;
import com.crm.service.BizProjectLevelService;
import com.crm.service.BizProjectService;
import com.crm.service.ConsultInfoManagePayService;
import com.crm.service.ConsultInfoManageService;
import com.crm.service.SysUserService;
import com.crm.util.SessionModel;
import com.crm.util.SessionUtils;

@Controller
@RequestMapping("/printController")
public class PrintController {
	@Autowired
	private ConsultInfoManageService consultService;
	@Autowired
	private ConsultInfoManagePayService  consultPayService;
	@Autowired
	private BizProjectService projectService;
	@Autowired
	private BizProjectLevelService bizProjectLevelService;
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private BizProductService bizProductService;
	@Autowired
	private BizConfigService bizConfigService;
	
	/**
	 * 
	 * @Description : 预览
	 * @author zealot
	 * @param @param infoManageId
	 * @param @param from
	 * @param @param request
	 * @param @return
	 * @return String
	 * @version 创建时间 : 2016年12月29日 上午10:13:05
	 * @thorws
	 */
	@RequestMapping("/index/{infoManageId}/{from}")
	public String index(@PathVariable("infoManageId")String infoManageId,@PathVariable("from")String from,HttpServletRequest request){
		ConsultInfoManage consultInfo =consultService.selectByPrimaryKey2(infoManageId);
		ConsultInfoManagePay payInfoData= consultPayService.getPaySumByInfoId(infoManageId);
		BizProject project =consultInfo==null?null:projectService.selectByPrimaryKey(consultInfo.getProjectId());
		BizProjectLevel projectLevel =consultInfo==null?null:bizProjectLevelService.selectByPrimaryKey(consultInfo.getProjectLevelId());
		BizProduct product = bizProductService.selectByPrimaryKey(consultInfo.getClassId());
		List<BizConfig> config = bizConfigService.selectUsByPrimaryKey(product.getProductDesc());
		SysUser t = new SysUser();
		t.setCompanyId(consultInfo.getDepartmentId1());
		SysUser sy = (SysUser)SessionUtils.get(SessionModel.SESSION_USER);
		request.setAttribute("sy", sy);
		request.setAttribute("userList", sysUserService.selectByUs(t));
		request.setAttribute("consultInfo", consultInfo);
		request.setAttribute("payInfoData", payInfoData);
		request.setAttribute("project", project);
		request.setAttribute("config", config);
		request.setAttribute("projectLevel", projectLevel);
		return "consultcenter/"+from;
	}
	
	/**
	 * 
	 * @Description : 打印
	 * @author zealot
	 * @param @param infoManageId
	 * @param @param from
	 * @param @param userId
	 * @param @param request
	 * @param @return
	 * @return String
	 * @version 创建时间 : 2016年12月29日 上午10:13:44
	 * @thorws
	 */
	@RequestMapping("/index/{infoManageId}/{from}/{userId}")
	public String index(@PathVariable("infoManageId")String infoManageId,@PathVariable("from")String from,@PathVariable("userId")String userId,HttpServletRequest request){
		ConsultInfoManage consultInfo =consultService.selectByPrimaryKey2(infoManageId);
		ConsultInfoManagePay payInfoData= consultPayService.getPaySumByInfoId(infoManageId);
		BizProject project =consultInfo==null?null:projectService.selectByPrimaryKey(consultInfo.getProjectId());
		BizProjectLevel projectLevel =consultInfo==null?null:bizProjectLevelService.selectByPrimaryKey(consultInfo.getProjectLevelId());
		BizProduct product = bizProductService.selectByPrimaryKey(consultInfo.getClassId());
		List<BizConfig> config = bizConfigService.selectUsByPrimaryKey(product.getProductDesc());
		SysUser t = new SysUser();
		t.setCompanyId(consultInfo.getDepartmentId1());
		List<SysUser> list = sysUserService.selectByUs(t);
		for(int i=0;i<list.size();i++){
			if(userId.equals(list.get(i).getUserId())){
				request.setAttribute("user", list.get(i).getRealName());
			}
		}
		SysUser sy = (SysUser)SessionUtils.get(SessionModel.SESSION_USER);
		request.setAttribute("sy", sy);
		request.setAttribute("consultInfo", consultInfo);
		request.setAttribute("payInfoData", payInfoData);
		request.setAttribute("project", project);
		request.setAttribute("config", config);
		request.setAttribute("projectLevel", projectLevel);
		return "consultcenter/"+from;
	}
}
